﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class Erase : MonoBehaviour {

    private RaycastHit hitInfo;
    private Texture2D tex2D;
    public Texture2D tex2D1;
    private Color mask = new Color(0, 0, 0, 0);
    public Color MaskColor = Color.gray;
    
    public float round = 100;
    
    

    private void Start()
    {
        //tex2D = new Texture2D(1024, 1024, TextureFormat.RGBA32, false);
        tex2D = new Texture2D(400, 400);

        for (int i = 0; i < tex2D.width; i++)
        {
            for (int j = 0; j < tex2D.height; j++)
            {
                //tex2D.SetPixel(i, j, MaskColor);
                tex2D.SetPixel(i, j, tex2D1.GetPixel(i,j));
            }
        }
        tex2D.Apply();
        Material material = new Material(Shader.Find("Unlit/Transparent"));
        material.mainTexture = tex2D;
        this.GetComponent<MeshRenderer>().material = material; 
        
    }
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            RayCastHide();
        }
    }
    void RayCastHide()
    {
        Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, Mathf.Infinity);

        if (hitInfo.collider != null)
        {
            Debug.Log(hitInfo.collider.gameObject.name);
            int x = (int)(hitInfo.textureCoord.x * tex2D.width);
            int y = (int)(hitInfo.textureCoord.y * tex2D.height);

            for (int i = 0; i < round / 2; i++)
            {
                for (int j = 0; j < round / 2; j++)
                {
                    if (x + i > tex2D.width || x - i < 0 || y + j > tex2D.height || y - j < 0)
                    {
                        continue;
                    }
                    tex2D.SetPixel(x + i, y + j, mask);
                    tex2D.SetPixel(x + i, y - j, mask);
                    tex2D.SetPixel(x - i, y + j, mask);
                    tex2D.SetPixel(x - i, y - j, mask);
                }
            }
            tex2D.Apply();
        }
        
       
    }
}
